# -*- coding: utf-8 -*-
#coding=utf-8
from method import *


def login():
    url = read_yaml(0, '登录')['url']
    headers = read_yaml(0, '登录')['headers']
    data = read_yaml(0, '登录')['data']
    expect = read_yaml(0, '登录')['expect']
    ree = send_requests(0, '登录', url, data, headers)
    a1 = json.loads(ree.text)
    token = a1['data']['token']
    return token
def audit_(user_id):
    conn = mysql('154')
    cursor = conn.cursor()
    # 查询流程ID
    sql5 = "SELECT audit_id FROM  audit  WHERE create_by=%s AND audit_status=0 ORDER BY create_time DESC LIMIT 1;"%(user_id)
    cursor.execute(sql5)
    conn.commit()
    result = cursor.fetchall()
    for i in result:
        audit_id = i[0]
    print('流程ID:' + str(audit_id))
    token = login()
    for i in range(10):
        # 查询接口
        url1 = read_yaml(4, '查询流程')['url']
        headers1 = read_yaml(4, '查询流程')['headers']
        headers1['Authorization'] = token
        data1 = read_yaml(4, '查询流程')['data']
        data1['auditId'] = audit_id
        expect1 = read_yaml(4, '查询流程')['expect']
        ree = send_requests(4, '查询流程', url=url1, data=data1, headers=headers1)
        a1 = json.loads(ree.text)

        auditNodeId = a1['data']['auditNodeId']
        auditOrder = a1['data']['auditOrder']
        # 审批接口
        url2 = read_yaml(5, '审批流程')['url']
        headers2 = read_yaml(5, '审批流程')['headers']
        headers2['Authorization'] = token
        expect2 = read_yaml(5, '审批流程')['expect']
        data2 = read_yaml(5, '审批流程')['data']
        data2['auditId'] = audit_id
        data2['auditNodeId'] = auditNodeId
        data2['auditOrder'] = auditOrder
        ree1 = send_requests(5, '审批流程', url=url2, data=data2, headers=headers2)
        a2 = json.loads(ree1.text)

        # 查询流程状态
        sql6 = "SELECT audit_status FROM audit WHERE audit_id=%s" % (audit_id)
        cursor.execute(sql6)
        conn.commit()
        result = cursor.fetchall()
        for i in result:
            audit_status = i[0]
        print(audit_status)
        if audit_status == 0:
            print("流程没审批完，请继续！")
            continue
        if audit_status == 1:
            print('流程审核通过啦！')
            break

if __name__=="__main__":
    write_yaml(r'D:\python\pythonProject\test\add_employee_requests.yaml', data1)
    # write_yaml(r'D:\python\pythonProject\test\add_employee_requests.yaml', data_uat1)
    audit_('886')




















